﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class WaveTemplateBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IFilterConfigDetailDA FilterConfigDA = DataAccess.CreateFilterConfigDetailDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();
        private static readonly IUserMessageDA userMessageDA = DataAccess.CreateUserMessageDA();
        private static readonly IWaveDA waveDA = DataAccess.CreateWaveDA();
        private static readonly IWaveDetailDA waveDetail = DataAccess.CreateWaveDetailDA();
        private static readonly IWaveTemplateDA waveTemplateDA = DataAccess.CreateWaveTemplateDA();

        public WaveTemplateBLL()
        {
            this.pubParasInfo = null;
        }

        public WaveTemplateBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WaveTemplateBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string CreatedWaveByWaveTemplate(string waveTemplateID)
        {
            string temp = string.Empty;
            List<OrderDetailInfo> orderList = this.PreviewWaveByWaveTemplate(waveTemplateID);
            if (orderList != null)
            {
                WaveTemplateInfo waveTemplateInfo = waveTemplateDA.GetWaveTemplateByID(waveTemplateID, this.dataBaseID[0]);
                Dictionary<string, List<OrderDetailInfo>> divtionGroup = new Dictionary<string, List<OrderDetailInfo>>();
                if ((((waveTemplateInfo.WaveGeneratedByOwner == "Y") || (waveTemplateInfo.WaveGeneratedByCarrier == "Y")) || (waveTemplateInfo.WaveGeneratedByCustomer == "Y")) || (waveTemplateInfo.WaveGeneratedByTransMode == "Y"))
                {
                    foreach (OrderDetailInfo info in orderList)
                    {
                        List<OrderDetailInfo> orderDetail;
                        string strGroupby = string.Empty;
                        if (waveTemplateInfo.WaveGeneratedByOwner == "Y")
                        {
                            strGroupby = info.OwnerID;
                        }
                        if (waveTemplateInfo.WaveGeneratedByCustomer == "Y")
                        {
                            strGroupby = strGroupby + info.ConsigneeID;
                        }
                        if (waveTemplateInfo.WaveGeneratedByCarrier == "Y")
                        {
                            strGroupby = strGroupby + info.CarrierID;
                        }
                        if (waveTemplateInfo.WaveGeneratedByTransMode == "Y")
                        {
                            strGroupby = strGroupby + info.TransportationMode;
                        }
                        if (!divtionGroup.ContainsKey(strGroupby))
                        {
                            orderDetail = new List<OrderDetailInfo> {
                                info
                            };
                            divtionGroup.Add(strGroupby, orderDetail);
                        }
                        else
                        {
                            orderDetail = new List<OrderDetailInfo>();
                            divtionGroup.TryGetValue(strGroupby, out orderDetail);
                            divtionGroup.Remove(strGroupby);
                            orderDetail.Add(info);
                            divtionGroup.Add(strGroupby, orderDetail);
                        }
                    }
                }
                else
                {
                    divtionGroup.Add("orderDetail", orderList);
                }
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                string strWaveID = string.Empty;
                try
                {
                    foreach (KeyValuePair<string, List<OrderDetailInfo>> info in divtionGroup)
                    {
                        WaveInfo waveInfo = new WaveInfo();
                        strWaveID = commonMethod.GenNextNumStr(dataBase, tran, "WAVE", this.dataBaseID[0]);
                        waveInfo.WaveID = strWaveID;
                        waveInfo.WaveType = "";
                        waveInfo.PalletPickDispatchMethod = "";
                        waveInfo.PiecePickDispatchMethod = "";
                        waveInfo.AllowConsolidation = "";
                        waveInfo.AutoBatch = "";
                        waveInfo.BatchFlag = "";
                        waveInfo.CasePickDispatchMethod = "";
                        waveInfo.ExternalWaveID = "";
                        waveInfo.LabelsPerCase = 0;
                        waveInfo.LabelsPerPallet = 0;
                        waveInfo.Descr = "波次模板" + waveTemplateID + "生成！";
                        waveInfo.CreatedBy = this.pubParasInfo.UserID;
                        waveInfo.Status = "New";
                        waveInfo.UpdatedBy = waveInfo.CreatedBy;
                        waveInfo.CreatedDate = DateTime.Now;
                        waveInfo.UpdatedDate = DateTime.Now;
                        if (waveDA.InsertWave(dataBase, tran, waveInfo, this.dataBaseID[0]) > 0)
                        {
                            List<OrderDetailInfo> orderDetailDivtion = info.Value;
                            foreach (OrderDetailInfo infoDetail in orderDetailDivtion)
                            {
                                WaveDetailInfo wavedetail;
                                wavedetail = new WaveDetailInfo();
                                wavedetail.WaveID = strWaveID;
                                wavedetail.OrderID = infoDetail.OrderID;
                                wavedetail.OrderLineID = infoDetail.LineID;
                                wavedetail.ProcessStatus = infoDetail.Status;
                                wavedetail.CreatedBy = this.pubParasInfo.UserID;
                                wavedetail.UpdatedBy = wavedetail.CreatedBy;
                                if (waveDetail.InsertWaveDetail(dataBase, tran, wavedetail, this.dataBaseID[0]) > 0)
                                {
                                    strWaveID = wavedetail.WaveID;
                                    orderDetailDA.UpdateOrderDetailBatchFlag(dataBase, tran, "Y", wavedetail.OrderID, wavedetail.OrderLineID.ToString(), this.dataBaseID[0]);
                                }
                            }
                            new WaveBLL(this.pubParasInfo).UpdateWaveStatusByOrderID(dataBase, tran, strWaveID);
                            SysSettingInfo sysSettinginfo = sysSettingDA.GetSysSettingByID("WaveTemplateAjax", this.dataBaseID[0], this.pubParasInfo);
                            if ((sysSettinginfo != null) && (sysSettinginfo.Value == "Y"))
                            {
                                UserMessageInfo usermeassageInfo;
                                usermeassageInfo = new UserMessageInfo();
                                usermeassageInfo.FromName = "";
                                usermeassageInfo.FromUserID = "";
                                usermeassageInfo.IsActive = "Y";
                                usermeassageInfo.IsRead = "N";
                                usermeassageInfo.MessageContent = "生成波次单" + temp;
                                usermeassageInfo.MessageTitle = "波次模板生成数据";
                                usermeassageInfo.SystemCreated = "Y";
                                usermeassageInfo.ToName = "";
                                usermeassageInfo.ToUserID = this.pubParasInfo.UserID;
                                usermeassageInfo.CreatedBy = this.pubParasInfo.UserID;
                                usermeassageInfo.UpdatedBy = usermeassageInfo.CreatedBy;
                                userMessageDA.InsertUserMessage(dataBase, tran, usermeassageInfo, new List<string> { "sysadm." });
                            }
                        }
                        temp = temp + strWaveID + ";";
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                finally
                {
                    conn.Close();
                }
            }
            return temp;
        }

        public string DeleteWaveTemplate(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (waveTemplateDA.DeleteWaveTemplate(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        temp = temp + IDS[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllWaveTemplateInfo()
        {
            return waveTemplateDA.GetAllWaveTemplate(this.dataBaseID[0]);
        }

        public WaveTemplateInfo GetWaveTemplateByID(string id)
        {
            return waveTemplateDA.GetWaveTemplateByID(id, this.dataBaseID[0]);
        }

        public DataSet GetWaveTemplateByQueryList(WaveTemplateQueryEntity waveQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(waveQuery.WaveTemplateID))
            {
                sqlWhere.Add(" WAVE_TEMPLATE_ID " + waveQuery.WaveTemplateID);
            }
            if (!string.IsNullOrEmpty(waveQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + waveQuery.WaveTemplateID);
            }
            return waveTemplateDA.GetWaveTemplateByQueryList(sqlWhere, waveQuery, this.dataBaseID[0]);
        }

        public string InsertWaveTemplate(WaveTemplateInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveTemplateID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(obj.WaveTemplateID))
                {
                    obj.WaveTemplateID = commonMethod.GenNextNumStr(dataBase, tran, "WaveTemplate", this.dataBaseID[0]);
                }
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (waveTemplateDA.CheckWaveTemplateIDUnique(dataBase, tran, obj.WaveTemplateID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F7300_001", this.pubParasInfo.UserLanguage, new List<string> { obj.WaveTemplateID });
                }
                if (waveTemplateDA.InsertWaveTemplate(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    strWaveTemplateID = obj.WaveTemplateID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveTemplateID;
        }

        public List<OrderDetailInfo> PreviewWaveByWaveTemplate(string waveTemplateID)
        {
            List<OrderDetailInfo> orderDetailList = null;
            DateTime CS00005;
            WaveTemplateInfo waveTemplateInfo = waveTemplateDA.GetWaveTemplateByID(waveTemplateID, this.dataBaseID[0]);
            if ((waveTemplateInfo == null) || !(waveTemplateInfo.IsActive == "Y"))
            {
                throw new Exception("未找到数据源!");
            }
            List<string> strWhere = new List<string>();
            if (!(waveTemplateInfo.RunMode == "Manual"))
            {
                throw new Exception("模板" + waveTemplateID + "为自动运行模式,无法进行手动操作!");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.WaveSelection))
            {
                List<FilterStatementInfo> statmentList = FilterConfigDA.GetFilterStatementByID("WaveSel", waveTemplateInfo.WaveSelection, this.dataBaseID[0]);
                if ((statmentList != null) && (statmentList.Count > 0))
                {
                    foreach (FilterStatementInfo info in statmentList)
                    {
                        string strLeft = string.Empty;
                        string strRight = string.Empty;
                        for (int l = 0; info.LeftParen > l; l++)
                        {
                            strLeft = strLeft + "(";
                        }
                        for (int r = 0; info.RightParen > r; r++)
                        {
                            strRight = strRight + ")";
                        }
                        strWhere.Add(strLeft + info.AndOr + "  " + info.Attribute.Replace("@_@", "") + "  " + info.Operand + " " + info.LiteralValue + strRight + " ");
                    }
                }
            }
            string strMaxNum = waveTemplateInfo.MaxOrders.ToString();
            DateTime dtAfter = new DateTime();
            DateTime dtBefore = new DateTime();
            if (waveTemplateInfo.DaysBefore > 0)
            {
                dtBefore = DateTime.Now;
                dtBefore.AddDays((double) (waveTemplateInfo.DaysBefore * -1));
            }
            if (waveTemplateInfo.HoursBefore > 0)
            {
                CS00005 = new DateTime();
                if (dtBefore == CS00005)
                {
                    dtBefore = DateTime.Now;
                }
                dtBefore.AddHours((double) (waveTemplateInfo.HoursBefore * -1));
            }
            if (waveTemplateInfo.MinutesBefore > 0)
            {
                CS00005 = new DateTime();
                if (dtBefore == CS00005)
                {
                    dtBefore = DateTime.Now;
                }
                dtBefore.AddMinutes((double) (waveTemplateInfo.MinutesBefore * -1));
            }
            if (waveTemplateInfo.DaysAfter > 0)
            {
                dtAfter = new DateTime();
                dtAfter.AddDays((double) waveTemplateInfo.DaysAfter);
            }
            if (waveTemplateInfo.HoursAfter > 0)
            {
                CS00005 = new DateTime();
                if (dtAfter == CS00005)
                {
                    dtAfter = DateTime.Now;
                }
                dtAfter.AddHours((double) waveTemplateInfo.HoursAfter);
            }
            if (waveTemplateInfo.MinutesAfter > 0)
            {
                CS00005 = new DateTime();
                if (dtAfter == CS00005)
                {
                    dtAfter = DateTime.Now;
                }
                dtAfter.AddMinutes((double) waveTemplateInfo.MinutesAfter);
            }
            CS00005 = new DateTime();
            if ((dtAfter != CS00005) && (dtBefore != (CS00005 = new DateTime())))
            {
                strWhere.Add(string.Concat(new object[] { " O.ORDER_DATE  BETWEEN '", dtBefore, "' AND '", dtAfter, "'" }));
            }
            else
            {
                CS00005 = new DateTime();
                if (dtAfter != CS00005)
                {
                    strWhere.Add(" O.ORDER_DATE < '" + dtAfter + "'");
                }
                if (dtBefore != new DateTime())
                {
                    strWhere.Add(" O.ORDER_DATE > '" + dtBefore + "'");
                }
            }
            string strOwner = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.OwnerIncludes))
            {
                strOwner = "'" + waveTemplateInfo.OwnerIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.OWNER_ID IN ( " + strOwner + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.OwnerExcludes))
            {
                strWhere.Add("  O.OWNER_ID NOT IN ( " + ("'" + waveTemplateInfo.OwnerExcludes.Replace(",", "','") + "'") + ")");
            }
            string strCustomer = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.CustomerIncludes))
            {
                strCustomer = "'" + waveTemplateInfo.CustomerIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.CONSIGNEE_ID  IN ( " + strCustomer + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.CustomerExcludes))
            {
                strWhere.Add("  O.CONSIGNEE_ID NOT IN ( " + ("'" + waveTemplateInfo.CustomerExcludes.Replace(",", "','") + "'") + ")");
            }
            string strCarrier = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.CarrierIncludes))
            {
                strCarrier = "'" + waveTemplateInfo.CarrierIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.CARRIER_ID  IN ( " + strCarrier + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.CustomerExcludes))
            {
                strWhere.Add("  O.CARRIER_ID  NOT IN ( " + ("'" + waveTemplateInfo.CustomerExcludes.Replace(",", "','") + "'") + ")");
            }
            string strTransMode = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.TransModeIncludes))
            {
                strTransMode = "'" + waveTemplateInfo.TransModeIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.TRANSPORTATION_MODE  IN ( " + strTransMode + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.TransModeExcludes))
            {
                strWhere.Add("  O.CARRIER_ID  NOT IN ( " + ("'" + waveTemplateInfo.TransModeExcludes.Replace(",", "','") + "'") + ")");
            }
            string strRoute = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.RouteIncludes))
            {
                strRoute = "'" + waveTemplateInfo.RouteIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.Route  IN ( " + strRoute + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.RouteExcludes))
            {
                strWhere.Add("  O.Route  NOT IN ( " + ("'" + waveTemplateInfo.RouteExcludes.Replace(",", "','") + "'") + ")");
            }
            string strTrailerNum = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.VehicleIncludes))
            {
                strTrailerNum = "'" + waveTemplateInfo.VehicleIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.Trailer_Number  IN ( " + strTrailerNum + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.VehicleExcludes))
            {
                strWhere.Add("  O.Trailer_Number  NOT IN ( " + ("'" + waveTemplateInfo.VehicleExcludes.Replace(",", "','") + "'") + ")");
            }
            string strDriver = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.DriverIncludes))
            {
                strDriver = "'" + waveTemplateInfo.DriverIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.DRIVER_NAME  IN ( " + strDriver + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.DriverExcludes))
            {
                strWhere.Add("  O.DRIVER_NAME  NOT IN ( " + ("'" + waveTemplateInfo.DriverExcludes.Replace(",", "','") + "'") + ")");
            }
            string strProv = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.ProvinceIncludes))
            {
                strProv = "'" + waveTemplateInfo.ProvinceIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.type  IN ( " + strProv + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.ProvinceExcludes))
            {
                strWhere.Add("  O.C_Prov  NOT IN ( " + ("'" + waveTemplateInfo.ProvinceExcludes.Replace(",", "','") + "'") + ")");
            }
            string strCiyt = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.CityIncludes))
            {
                strCiyt = "'" + waveTemplateInfo.CityIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.C_CITY  IN ( " + strCiyt + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.CityExcludes))
            {
                strWhere.Add("  O.C_CITY  NOT IN ( " + ("'" + waveTemplateInfo.CityExcludes.Replace(",", "','") + "'") + ")");
            }
            string strOrderType = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.OrderTypeIncludes))
            {
                strOrderType = "'" + waveTemplateInfo.OrderTypeIncludes.Replace(",", "','") + "'";
                strWhere.Add("  O.TYPE  IN ( " + strOrderType + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.OrderTypeExcludes))
            {
                strWhere.Add("  O.TYPE  NOT IN ( " + ("'" + waveTemplateInfo.OrderTypeExcludes.Replace(",", "','") + "'") + ")");
            }
            strWhere.Add("  O.STATUS  ='NEW'");
            if (!string.IsNullOrEmpty(waveTemplateInfo.OrderStatusIncludes))
            {
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.OrderStatusExcludes))
            {
            }
            string strSku = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.SkuIncludes))
            {
                if (waveTemplateInfo.SkuIncludes.Substring(waveTemplateInfo.SkuIncludes.Length) == ",")
                {
                    waveTemplateInfo.SkuIncludes = waveTemplateInfo.SkuIncludes.Substring(0, waveTemplateInfo.SkuIncludes.Length - 1);
                }
                strSku = "'" + waveTemplateInfo.SkuIncludes.Replace(",", "','") + "'";
                strWhere.Add("  OD.SKU_ID  IN ( " + strSku + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.SkuExcludes))
            {
                if (waveTemplateInfo.SkuExcludes.Substring(waveTemplateInfo.SkuExcludes.Length) == ",")
                {
                    waveTemplateInfo.SkuExcludes = waveTemplateInfo.SkuExcludes.Substring(0, waveTemplateInfo.SkuExcludes.Length - 1);
                }
                strWhere.Add("  OD.SKU_ID  NOT IN ( " + ("'" + waveTemplateInfo.SkuExcludes.Replace(",", "','") + "'") + ")");
            }
            string strSkuClass = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.SkuClassIncludes))
            {
                if (waveTemplateInfo.SkuClassIncludes.Substring(waveTemplateInfo.SkuClassIncludes.Length) == ",")
                {
                    waveTemplateInfo.SkuClassIncludes = waveTemplateInfo.SkuClassIncludes.Substring(0, waveTemplateInfo.SkuClassIncludes.Length - 1);
                }
                strSkuClass = "'" + waveTemplateInfo.SkuClassIncludes.Replace(",", "','") + "'";
                strWhere.Add("  SKU.SKU_CLASS_ID  IN ( " + strSkuClass + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.SkuClassExcludes))
            {
                if (waveTemplateInfo.SkuClassExcludes.Substring(waveTemplateInfo.SkuClassExcludes.Length) == ",")
                {
                    waveTemplateInfo.SkuClassExcludes = waveTemplateInfo.SkuClassExcludes.Substring(0, waveTemplateInfo.SkuClassExcludes.Length - 1);
                }
                strWhere.Add("  OD.SKU_CLASS_ID  NOT IN ( " + ("'" + waveTemplateInfo.SkuClassExcludes.Replace(",", "','") + "'") + ")");
            }
            string strSkuStyle = string.Empty;
            if (!string.IsNullOrEmpty(waveTemplateInfo.SkuStyleIncludes))
            {
                if (waveTemplateInfo.SkuStyleIncludes.Substring(waveTemplateInfo.SkuStyleIncludes.Length) == ",")
                {
                    waveTemplateInfo.SkuStyleIncludes = waveTemplateInfo.SkuStyleIncludes.Substring(0, waveTemplateInfo.SkuStyleIncludes.Length - 1);
                }
                strSkuStyle = "'" + waveTemplateInfo.SkuStyleIncludes.Replace(",", "','") + "'";
                strWhere.Add("  SKU.SKU_STYLE  IN ( " + strSkuStyle + ")");
            }
            if (!string.IsNullOrEmpty(waveTemplateInfo.SkuStyleExcludes))
            {
                if (waveTemplateInfo.SkuStyleExcludes.Substring(waveTemplateInfo.SkuStyleExcludes.Length) == ",")
                {
                    waveTemplateInfo.SkuStyleExcludes = waveTemplateInfo.SkuStyleExcludes.Substring(0, waveTemplateInfo.SkuStyleExcludes.Length - 1);
                }
                strWhere.Add("  SKU.SKU_STYLE  NOT IN ( " + ("'" + waveTemplateInfo.SkuStyleExcludes.Replace(",", "','") + "'") + ")");
            }
            if (waveTemplateInfo.OrderQty > 0)
            {
                strWhere.Add("  OD.ORIGINAL_QTY =" + waveTemplateInfo.OrderQty + "   ");
            }
            if (waveTemplateInfo.SingleItemWave == "Y")
            {
                strWhere.Add("  ORDERCOUNT.TOTALCOUNT = 1   ");
            }
            if (waveTemplateInfo.MultiItemWave == "Y")
            {
                strWhere.Add("  ORDERCOUNT.TOTALCOUNT > 1   ");
            }
            if (waveTemplateInfo.MinOrderLines > 0)
            {
                strWhere.Add(" ORDERCOUNT.TOTALCOUNT >= '" + waveTemplateInfo.MinOrderLines + "'");
            }
            if (waveTemplateInfo.MaxOrderLines > 0)
            {
                strWhere.Add(" ORDERCOUNT.TOTALCOUNT <= '" + waveTemplateInfo.MinOrderLines + "'");
            }
            if (waveTemplateInfo.MaxSkus > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALSKUCOUNT <= '" + waveTemplateInfo.MaxSkus + "'");
            }
            if (waveTemplateInfo.MinSkus > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALSKUCOUNT >= '" + waveTemplateInfo.MinSkus + "'");
            }
            if (waveTemplateInfo.MaxQty > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALQTY <= '" + waveTemplateInfo.MaxQty + "'");
            }
            if (waveTemplateInfo.MinQty > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALQTY >= '" + waveTemplateInfo.MinQty + "'");
            }
            if (waveTemplateInfo.MaxWeight > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALWGT <= '" + waveTemplateInfo.MaxWeight + "'");
            }
            if (waveTemplateInfo.MinWeight > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALWGT >= '" + waveTemplateInfo.MinWeight + "'");
            }
            if (waveTemplateInfo.MaxVolume > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALCUBE <= '" + waveTemplateInfo.MaxVolume + "'");
            }
            if (waveTemplateInfo.MinVolume > 0)
            {
                strWhere.Add("  ORDERSKUCOUNT.TOTALCUBE >= '" + waveTemplateInfo.MinVolume + "'");
            }
            strWhere.Add(" OD.BATCH_FLAG = 'N'");
            orderDetailList = orderDetailDA.GetOrderDetailInfoByWaveTemplate(strWhere, strMaxNum, this.dataBaseID[0], this.pubParasInfo);
            if ((orderDetailList.Count > 0) && ((waveTemplateInfo.MinOrders > 0) && (orderDetailList.Count < waveTemplateInfo.MinOrders)))
            {
                throw new Exception("模板" + waveTemplateID + " 根据最小订单限制无法生成波次单");
            }
            return orderDetailList;
        }

        public string UpdateWaveTemplate(WaveTemplateInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveTemplateID = string.Empty;
            try
            {
                if (waveTemplateDA.UpdateWaveTemplate(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    strWaveTemplateID = obj.WaveTemplateID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveTemplateID;
        }
    }
}

